package com.winona.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.R;



public class ListViewAdpter extends BaseAdapter {
	public ArrayList<String> data; // 数据源
	private Context context;
	private float downX; // 点下时候获取的x坐标
	private float upX; // 手指离开时候的x坐标
	private Button button; // 用于执行删除的button
	private Animation animation; // 删除时候的动画
	private View view;

	public ListViewAdpter(ArrayList<String> data, Context context) {
		this.data = data;
		this.context = context;
		animation = AnimationUtils.loadAnimation(context, R.anim.push_out); // 用xml获取一个动画
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.sensivive_item, null);
			holder = new ViewHolder();
			// holder.textView = (TextView) convertView.findViewById(R.id.text);
			holder.sensitive_img = (ImageView) convertView
					.findViewById(R.id.sensitive_img);
			holder.sensitive_title = (TextView) convertView
					.findViewById(R.id.sensitive_name);
//			holder.sensitive_delete = (Button) convertView
//					.findViewById(R.id.sensitive_delete);
			holder.sensitive_description = (TextView) convertView
					.findViewById(R.id.sensitive_description);
			holder.sensitive_jiantou = (ImageView) convertView
					.findViewById(R.id.sensitive_jiantou);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		convertView.setOnTouchListener(new OnTouchListener() { // 为每个item设置setOnTouchListener事件

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						final ViewHolder holder = (ViewHolder) v.getTag(); // 获取滑动时候相应的ViewHolder，以便获取button按钮
						switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN: // 手指按下
							downX = event.getX(); // 获取手指x坐标
							if (button != null) {
								button.setVisibility(View.GONE); // 影藏显示出来的button
							}
							break;
						case MotionEvent.ACTION_UP: // 手指离开
							upX = event.getX(); // 获取x坐标值
							break;
						}

						if (holder.sensitive_delete != null) {
							if (Math.abs(downX - upX) >100) { // 2次坐标的绝对值如果大于35，就认为是左右滑动

								holder.sensitive_delete
										.setVisibility(View.VISIBLE); // 显示删除button
								button = holder.sensitive_delete; // 赋值给全局button，一会儿用
								view = v; // 得到itemview，在上面加动画
								return true; // 终止事件
							}
							return false; // 释放事件，使onitemClick可以执行
						}
						return false;
					}

				});

		holder.sensitive_delete.setOnClickListener(new OnClickListener() { // 为button绑定事件

					@Override
					public void onClick(View v) {

						if (button != null) {
							button.setVisibility(View.GONE); // 点击删除按钮后，影藏按钮
							deleteItem(view, position); // 删除数据，加动画
						}

					}
				});
		holder.sensitive_title.setText(data.get(position)); // 显示数据
		return convertView;
	}

	public void deleteItem(View view, final int position) {
		view.startAnimation(animation); // 给view设置动画
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) { // 动画执行完毕
				data.remove(position); // 把数据源里面相应数据删除
				notifyDataSetChanged();

			}
		});

	}

	static class ViewHolder {
		private Button sensitive_delete; // 删除按钮
		private ImageView sensitive_img;
		private TextView sensitive_title;
		private TextView sensitive_description;
		private ImageView sensitive_jiantou;
	}

}
